package cn.mylearn.product.controller;

import java.util.Arrays;
import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.mylearn.product.entity.CategoryEntity;
import cn.mylearn.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.mylearn.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author jianghaiyang
 * @email betterjhy@163.com
 * @date 2021-06-28 20:58:06
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 查出所有的分类和子分类，以树形的结构组装起来
     *
     */
    @RequestMapping("/list/tree")
     public R queryList(){
        List<CategoryEntity> entities = categoryService.listWithTree();



        return R.ok().put("data",entities);
     }





    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		//CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        CategoryEntity categoryServiceById = categoryService.getById(id);

        return R.ok().put("data", categoryServiceById);
    }

    /**
     * 保存
     */
    /*@RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }*/
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity categoryEntity){
        categoryService.save(categoryEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryEntity categoryEntity){
		//categoryBrandRelationService.updateById(categoryBrandRelation);
        categoryService.updateById(categoryEntity);
        return R.ok();
    }

    /**
     * 删除
     * @RequestBody 是获取请求体中的值，只有post请求有请求体，get请求没有
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
        //这是自动生成的删除，不过一般我们还是用自己写的，因为我们需要判断要删除的是否被引用了，如果引用了就不能删除

		//categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        categoryService.removeMenusByIds(Arrays.asList(ids));

        return R.ok();
    }

}
